<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试图片压缩功能</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }
        input, select {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        input:focus, select:focus {
            outline: none;
            border-color: #667eea;
        }
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        .result {
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            font-family: 'Courier New', monospace;
            font-size: 13px;
            white-space: pre-wrap;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .loading {
            text-align: center;
            padding: 20px;
        }
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .file-info {
            margin-top: 10px;
            padding: 10px;
            background: #e9ecef;
            border-radius: 5px;
            font-size: 12px;
        }

        /* 导航栏样式 */
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 15px 30px;
            border-radius: 15px;
            margin-bottom: 20px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
        }

        .nav-brand {
            font-size: 1.2rem;
            font-weight: 600;
            color: #333;
        }

        .nav-links {
            display: flex;
            gap: 20px;
        }

        .nav-link {
            text-decoration: none;
            color: #666;
            padding: 8px 16px;
            border-radius: 8px;
            transition: all 0.3s ease;
            font-weight: 500;
        }

        .nav-link:hover {
            background: #667eea;
            color: white;
            transform: translateY(-2px);
        }

        .nav-link.active {
            background: #667eea;
            color: white;
        }

        @media (max-width: 768px) {
            .navbar {
                flex-direction: column;
                gap: 15px;
                padding: 20px;
            }
            
            .nav-links {
                flex-wrap: wrap;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 导航栏 -->
        <nav class="navbar">
            <div class="nav-brand">SpringBoot Excel Demo</div>
            <div class="nav-links">
                <a href="/" class="nav-link">🏠 主页面</a>
                <a href="/test_downloads.html" class="nav-link">📤 异步导出测试</a>
                <a href="/test_image.html" class="nav-link active">🖼️ 图片压缩测试</a>
                <a href="/test_easyexcel_import.html" class="nav-link">📋 EasyExcel图片导入测试</a>
            </div>
        </nav>

        <h1>🖼️ 图片压缩功能测试</h1>
        <p>测试图片压缩功能是否正常工作</p>

        <div class="form-group">
            <label for="imageFile">选择图片文件</label>
            <input type="file" id="imageFile" accept="image/*" onchange="showFileInfo()">
            <div id="fileInfo" class="file-info" style="display: none;"></div>
        </div>

        <div class="form-group">
            <label for="targetSize">目标大小 (KB)</label>
            <input type="number" id="targetSize" value="100" min="1" max="1000">
        </div>

        <button class="btn" onclick="testImageCompress()">测试图片压缩</button>

        <div id="result" class="result" style="display: none;"></div>
    </div>

    <script>
        function showFileInfo() {
            const file = document.getElementById('imageFile').files[0];
            const fileInfo = document.getElementById('fileInfo');

            if (file) {
                const sizeKB = (file.size / 1024).toFixed(2);
                fileInfo.innerHTML = `
                    <strong>文件信息:</strong><br>
                    文件名: ${file.name}<br>
                    文件大小: ${sizeKB} KB<br>
                    文件类型: ${file.type}
                `;
                fileInfo.style.display = 'block';
            } else {
                fileInfo.style.display = 'none';
            }
        }

        async function testImageCompress() {
            const resultDiv = document.getElementById('result');
            const file = document.getElementById('imageFile').files[0];
            const targetSize = document.getElementById('targetSize').value;

            if (!file) {
                resultDiv.style.display = 'block';
                resultDiv.className = 'result error';
                resultDiv.textContent = '❌ 请选择图片文件';
                return;
            }

            // 显示加载状态
            resultDiv.style.display = 'block';
            resultDiv.className = 'result';
            resultDiv.innerHTML = `
                <div class="loading">
                    <div class="spinner"></div>
                    正在压缩图片，请稍候...
                </div>
            `;

            const formData = new FormData();
            formData.append('file', file);
            formData.append('targetSizeKb', targetSize);

            try {
                console.log('开始压缩图片...');
                console.log('文件信息:', {
                    name: file.name,
                    size: file.size,
                    type: file.type
                });

                const response = await fetch('/api/image/compress', {
                    method: 'POST',
                    body: formData
                });

                console.log('响应状态:', response.status);

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const result = await response.json();
                console.log('压缩结果:', result);

                if (result.code === 0) {
                    const data = result.data;
                    const compressionRatio = ((1 - data.compressionRatio) * 100).toFixed(2);

                    resultDiv.className = 'result success';
                    resultDiv.innerHTML = `
                        ✅ 图片压缩成功!

                        📊 压缩结果:
                        - 原始大小: ${data.originalSize} KB
                        - 压缩后大小: ${data.compressedSize} KB
                        - 压缩比例: ${compressionRatio}%
                        - 文件路径: ${data.filePath}

                        📋 完整响应:
                        ${JSON.stringify(result, null, 2)}
                    `;
                } else {
                    resultDiv.className = 'result error';
                    resultDiv.textContent = `❌ 压缩失败: ${result.msg}`;
                }

            } catch (error) {
                console.error('压缩失败:', error);
                resultDiv.className = 'result error';
                resultDiv.innerHTML = `
                    ❌ 图片压缩失败!

                    🔍 错误信息: ${error.message}

                    💡 可能的原因:
                    1. 应用未启动或端口不正确
                    2. 网络连接问题
                    3. 服务器内部错误
                    4. 文件格式不支持

                    📋 详细错误:
                    ${error.stack || error.message}
                `;
            }
        }
    </script>
</body>
</html>
